package com.hunnyhu.tools;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import android.content.Context;
import android.content.Intent;
import android.text.TextPaint;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;

public class XAdapter extends BaseAdapter {
	protected Context context;
	protected LayoutInflater mInflater;
	protected int resource;
	protected List<Map<String, Object>> list;
	public XAdapter(Context context, List<Map<String, Object>> list, int resource) {
		this.context = context;
		this.resource = resource;
		this.list = list;
		this.mInflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
	}

	@Override
	public int getCount() {
		if (null == list || list.isEmpty()) {
			return 0;
		}
		return list.size();
	}

	@Override
	public Map<String, Object> getItem(int position) {
		return list.get(position);
	}

	@Override
	public long getItemId(int position) {
		return position;
	}

	@Override
	public View getView(int position, View convertView, ViewGroup parent) {
		if (null == list || list.isEmpty()) {
			return convertView;
		}
        View v = null;
        if (convertView == null) {
            v = mInflater.inflate(resource, parent, false);
        } else {
            v = convertView;
        }
		return showDetails(v, getItem(position));
	}
	
	protected View showDetails(View view, Map<String, Object> map) {
		if (null == map) {
			map = new HashMap<String, Object>();
		}
		TextView checkDetails = (TextView)view.findViewById(R.id.checkDetails);
		String begin = (String)map.get(DBHelper.C_DATE_BEGIN);
		String end = (String)map.get(DBHelper.C_DATE_END);
		String checkIn = (null == begin) ? "暂无信息" : begin;
		String checkOut = (null == end) ? 
				"暂无信息" : end;
		ImageView imageView = (ImageView)view.findViewById(R.id.checkImage);
		if (null != begin && null != end) {
			imageView.setImageResource(R.drawable.check_status_ok);
		} else {
			imageView.setImageResource(R.drawable.check_status);
		}
		StringBuffer signInfo = new StringBuffer();
		signInfo.append("签到时间: ");
		signInfo.append(checkIn);
		signInfo.append("\n");
		signInfo.append("签退时间: ");
		signInfo.append(checkOut);
		TextPaint tp = checkDetails.getPaint();
		tp.setFakeBoldText(true);
		checkDetails.setText(signInfo.toString());
		TextView checkLogs = (TextView)view.findViewById(R.id.checkLogs);
		final String id = (String)map.get(DBHelper.C_ID);
		if (null == id) {
			checkLogs.setVisibility(View.GONE);
		} else {
			checkLogs.setVisibility(View.VISIBLE);
			String dateCreated = (null == map.get(DBHelper.C_DATE_CREATED) ? 
					"暂无信息" : (String)map.get(DBHelper.C_DATE_CREATED));
			String lastUpdated = (null == map.get(DBHelper.C_LAST_UPDATED) ? 
					"暂无信息" : (String)map.get(DBHelper.C_LAST_UPDATED));
			StringBuffer logInfo = new StringBuffer();
			logInfo.append("记录时间: ");
			logInfo.append(dateCreated);
			logInfo.append("\n");
			logInfo.append("更新时间: ");
			logInfo.append(lastUpdated);
			checkLogs.setText(logInfo.toString());
		}
		view.setOnClickListener(new OnClickListener() {
			@Override
			public void onClick(View v) {
				Intent intent = new Intent(); 
				intent.putExtra("id", id);
				intent.setClass(context, DetailsActivity.class);  
				context.startActivity(intent);
//				Toast.makeText(context, "点击功能待开发", Toast.LENGTH_SHORT).show();
			}
		});
		return view;
	}

}
